import DWC_DCMenuItem from "./DWC_DCMenuItem/DWC_DCMenuItem";

const { ccclass, property } = cc._decorator;
/** 复杂聊天菜单 create by tm */
@ccclass
export default class DWC_DCMenu extends cc.Component {
    //-----------组件区----------
    @property(cc.ScrollView)
    scroll_menu: cc.ScrollView = null;
    @property(cc.Prefab)
    menuItem_pfb: cc.Prefab = null;
    //-----------变量区----------
    menuParent: cc.Node = null;
    onLoad() {
        this.addEvent();
    }
    /** 添加事件 */
    addEvent(): void {
        this.node.on(`dwc_detailChat_touchMenuItem`, this.touchItem, this);//点击 menu item 
        this.node.on(`dwc_detailChat_deleteMenuItem`, this.touchDeleteItem, this);//点击移除 menu item 
    }
    /** 移除事件 */
    removeEvent(): void {
        this.node.off(`dwc_detailChat_touchMenuItem`, this.touchItem, this);
        this.node.off(`dwc_detailChat_deleteMenuItem`, this.touchDeleteItem, this);
    }
    /** 初始化(外界调用) */
    init(_args): void {
        this.menuParent = _args.menuParent;
    }
    /** 添加菜单item */
    addMenuItem(_menu: {
        menuName: string,//聊天名
        isChoose: boolean,//是否选中
        isDelete: boolean,//是否可移除
        type: number,//聊天类型 1 私聊消息(玩家) 2 桌子消息(房间) 3 大厅消息
        chatId: any//聊天id chatType == 1时,为接收者 rid; chatType == 2时,为接收者 桌子ID; chatType == 3时,为接收者 所属代理ID
    }): DWC_DCMenuItem {
        let _node = cc.instantiate(this.menuItem_pfb);
        let _item: DWC_DCMenuItem = _node.getComponent(_node.name);
        _node.parent = this.scroll_menu.content;
        _node.position = cc.v2(0, 0);
        _item.init({
            menuNode: this.node,
            menuName: _menu.menuName,
            isChoose: _menu.isChoose,
            isDelete: _menu.isDelete,
            type: _menu.type,
            chatId: _menu.chatId,
            itemIdx: this.scroll_menu.content.childrenCount - 1
        });
        let _spaceY = this.scroll_menu.content.getComponent(cc.Layout).spacingY
        this.scroll_menu.content.height = this.scroll_menu.content.childrenCount * (_item.node.height + _spaceY) - _spaceY;
        return _item;
    }
    /** 设置选中 */
    setChoose(_itemIdx: number, _type: number, _chatId: any): void {
        //设置item Status
        this.scroll_menu.content.children.forEach((_v, _idx) => {
            let _item: DWC_DCMenuItem = _v.getComponent(_v.name);
            _item.setStatus(_idx == _itemIdx);
        });
        this.menuParent && this.menuParent.emit(`dwc_detailChat_touchMenu`, {
            idx: _itemIdx,
            type: _type,
            chatId: _chatId
        });
    }
    /** 点击item */
    touchItem(_evt): void {
        if (!_evt) return;
        this.setChoose(_evt.idx, _evt.type, _evt.chatId);
    }
    /** 点击移除menuItem */
    touchDeleteItem(_evt): void {
        if (!_evt) return;
        cc.vv.session.removePMenu(_evt.chatId, _evt.type);
        //设置item Status
        this.scroll_menu.content.children.forEach((_v, _idx) => {
            if (_idx > _evt.idx) {
                let _item2: DWC_DCMenuItem = _v.getComponent(_v.name);
                _item2.itemIdx--;
            }
        });
        if (_evt.isChoose) {//移除当前选中
            if (_evt.idx < 1) return;
            let _node = this.scroll_menu.content.children[_evt.idx - 1];
            let _item: DWC_DCMenuItem = _node.getComponent(_node.name);
            this.setChoose(_evt.idx - 1, _item.chatType, _item.chatId);
        }
    }
    /** 收到广播消息，设置menuitem*/
    setMenuItem(_menu: {
        menuName: string,
        chatId: any,
        chatType: number
    }): void {
        //是否已存在当前会话
        let _isItem: boolean = false;
        if (_menu.chatType != 1) {//大厅或房间
            _isItem = true;
            let _idx = 0;
            if (_menu.chatType == 2) _idx = 1;
            let _node = this.scroll_menu.content.children[_idx];
            let _item0: DWC_DCMenuItem = _node.getComponent(_node.name);
            _item0.setRed(true);
        } else {
            this.scroll_menu.content.children.forEach(_v => {
                let _item: DWC_DCMenuItem = _v.getComponent(_v.name);
                if (_item.chatId == _menu.chatId && _item.chatType == _menu.chatType) {//已存在聊天menu,消息提醒
                    _isItem = true;
                    _item.setRed(true);
                }
            });
        }
        //不存在则添加聊天menuitem,且能添加的都是私聊
        if (!_isItem) {
            let _menuItem = {
                menuName: _menu.menuName,
                isChoose: false,
                isDelete: _menu.chatType == 1,
                type: _menu.chatType,
                chatId: _menu.chatId
            }
            cc.vv.session.setPMenu(_menuItem);
            this.addMenuItem(_menuItem).setRed(true);
            this.showBtm();
        }
    }
    /** 显示最下方 */
    showBtm(): void {
        let _contenth: number = this.scroll_menu.content.height, _viewh: number = this.scroll_menu.node.height;
        this.scroll_menu.content.y = _contenth > _viewh ? Math.ceil(_contenth - _viewh / 2) : _viewh / 2;
    }
    /** 设置选中每一条并显示Menu */
    setChooseAndShowMenu(_chatId: any, _chatType: number): void {
        let _itemIdx = -1;
        this.scroll_menu.content.children.forEach((_v, _idx) => {
            let _menu = _v.getComponent(_v.name) as DWC_DCMenuItem;
            let _isChoose: boolean = _chatType == _menu.chatType && _chatId == _menu.chatId;
            _menu.setStatus(_isChoose);
            if (_isChoose) {
                _menu.setRed(false);
                this.menuParent && this.menuParent.emit(`dwc_detailChat_touchMenu`, {
                    idx: _idx,
                    type: _chatType,
                    chatId: _chatId
                });
                _itemIdx = _idx;
            }
        });
        //在最下方显示当前选择
        let _contenth: number = this.scroll_menu.content.height, _viewh: number = this.scroll_menu.node.height;
        if (_itemIdx >= 0 && _contenth > _viewh) {
            let _h: number = this.scroll_menu.content.children[0].height, _spaceY = this.scroll_menu.content.getComponent(cc.Layout).spacingY
            _contenth -= (_itemIdx + 1) * (_h + _spaceY) - _spaceY;
            this.scroll_menu.content.y = Math.ceil(_contenth - _viewh / 2);
        }
    }
    onDestroy() {
        this.removeEvent();
    }
}
